<template>
	<view class="flex-column-view">
		<!-- TODO 骨架屏 -->
		<image v-if="userInfo.status === 'new'" class="bg-img" src="https://free.picui.cn/free/2025/10/12/68eb494af18e0.png" mode="widthFix"></image>
		<image v-else-if="userInfo.status === 'old'" class="bg-img" src="https://free.picui.cn/free/2025/10/12/68eb494b1f102.png" mode="widthFix"></image>
		<view v-else-if="userInfo.status === 'vip'" class="menu">
			<view class="card-box">
				<navigator url="/pages/order/list/index" class="order-card">
					<image class="bg" src="https://free.picui.cn/free/2025/10/12/68eb56db5a9ab.png" mode="aspectFill"></image>
					<view class="label">我的订单</view>
					<text class="btn">
						去查看
						<nut-icon name="rect-right"></nut-icon>
						<nut-icon name="rect-right"></nut-icon>
					</text>
				</navigator>
				<view class="right-box">
					<view class="free-card haptics-feedback" @click="activityVisible = true">
						<image class="bg" src="https://free.picui.cn/free/2025/10/12/68eb56da2c10c.png" mode="aspectFill"></image>
						<view class="label">我要免单</view>
						<view class="desc">邀请新客享免单</view>
					</view>
					<view class="share-card haptics-feedback" @click="activityVisible = true">
						<image class="bg" src="https://free.picui.cn/free/2025/10/12/68eb56d9f2509.png" mode="aspectFill"></image>
						<view class="label">分享得大礼</view>
						<view class="desc">多重好礼手拿软</view>
					</view>
				</view>
			</view>
		</view>
		<view class="flex-1 content-box">
			<nut-tabs v-model="tabValue" animated-time="0" @change="tabChange">
				<nut-tab-pane title="已分享" pane-key="1">
					<view class ="card-item" v-for="item in shareList" :key="item.activityId">
						<view class="header">
							<view class="name text-ellipsis">{{ item?.name }}</view>
							<view v-if="Number(item?.status) === 0" class="status underway">火热进行中</view>
							<view v-else class="status">已结束</view>
						</view>
						<view class="content">
							{{ item?.description }}
						</view>
						<nut-divider />
						<view class="footer">
							<view class="footer-left">
								<block v-if="item?.getMoney">
									<text class="lable">已获受益</text>
									<text class="value">+{{ item?.getMoney }}</text>
								</block>
							</view>
							<view class="footer-right">
								<view class="btn-look">
									<nut-button plain type="primary" @click="toActivityDetail(item?.activityId)">查看</nut-button>
								</view>
								<view v-if="Number(item?.status) === 0" class="btn-share">
									<nut-button open-type="share" @click="handleShare(item)">分享赚</nut-button>
								</view>
							</view>
						</view>
					</view>
					<Empty v-if="!shareList?.length" />
				</nut-tab-pane>
				<nut-tab-pane title="查看过" pane-key="2">
					<view class ="card-item" v-for="item in viewList" :key="item.activityId">
						<view class="header">
							<view class="name text-ellipsis">{{ item?.name }}</view>
							<view v-if="Number(item?.status) === 0" class="status underway">火热进行中</view>
							<view v-else class="status">已结束</view>
						</view>
						<view class="content">
							{{ item?.description }}
						</view>
						<nut-divider />
						<view class="footer">
							<view class="footer-left">
								<block v-if="item?.getMoney">
									<text class="lable">已获受益</text>
									<text class="value">+{{ item?.getMoney }}</text>
								</block>
							</view>
							<view class="footer-right">
								<view class="btn-look">
									<nut-button plain type="primary" @click="toActivityDetail(item?.activityId)">查看</nut-button>
								</view>
								<view v-if="Number(item?.status) === 0" class="btn-share">
									<nut-button open-type="share" @click="handleShare(item)">分享赚</nut-button>
								</view>
							</view>
						</view>
					</view>
					<Empty v-if="!viewList?.length" />
				</nut-tab-pane>
			</nut-tabs>
		</view>
	</view>
	
	<SelectActivity
		v-model:visible="activityVisible"
	/>
</template>

<script setup>
import Empty from '@/components/Empty/index.vue'
import SelectActivity from './components/SelectActivity.vue';
import { onShow, onLoad, onPullDownRefresh, onShareAppMessage } from '@dcloudio/uni-app'
import { ref, watch, computed } from 'vue';
import { useUserStore } from '@/store/user';
import { getActivityShareList, getActivityViewList } from '@/api/activity';
import { shareMarkApi } from '@/api/mark';
import storage from '@/utils/storage';

const userStore = useUserStore()
const userInfo = computed(() => userStore.userInfo)

const tabValue = ref('1')
const shareList = ref([])
const viewList = ref([])
const currentActivityData = ref({})

const getShareList = async () => {
	try {
		const res = await getActivityShareList()
		shareList.value = res.data
	} catch {
	}
}
getShareList()

const getViewList = async () => {
	try {
		const res = await getActivityViewList()
		viewList.value = res.data
	} catch {
	}
}

const tabChange = () => {
	tabValue.value === '1' ? getShareList() : getViewList()
}

onPullDownRefresh(() => {
	tabChange()
	uni.stopPullDownRefresh()
})

const activityVisible = ref(false)

watch(activityVisible, (val) => {
	if (val) {
		uni.hideTabBar()
	} else {
		setTimeout(() => {
			uni.showTabBar()
		}, 250)
	}
})

const toActivityDetail = (activityId) => {
	uni.navigateTo({
		url: `/pages/activity/detail/index?activityId=${activityId}`
	})
}

onShow(() => {
	userStore.getUserInfo()
	const index_tabValue = storage().get('index_tabValue')
	if (index_tabValue) {
		tabValue.value = index_tabValue
		tabChange()
	}
})

const handleShare = (item) => {
	currentActivityData.value = item
}

const shareMark = async () => {
	try {
		await shareMarkApi({
			shareUserId: userInfo.value?.id,
			shareActivityId: currentActivityData.value?.activityId
		})
	} catch {
	}
}

onShareAppMessage(() => {
	setTimeout(() => {
		shareMark()
		return {
			title: `邀请你参加${currentActivityData.value?.name}`,
			path: `/pages/activity/detail/index?activityId=${currentActivityData.value?.activityId}&shareUserId=${userInfo.value?.id}`,
			// imageUrl: ''
		}
	}, 100)
})
</script>

<style lang="scss" scoped>
	.bg-img {
		width: 100%;
		height: 520rpx;
	}
	.menu {
		width: 100%;
		height: 586rpx;
		background-image: url('https://free.picui.cn/free/2025/10/12/68eb56de828c2.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
		.card-box {
			height: 340rpx;
			margin: 172rpx 30rpx 0;
			display: flex;
			gap: 30rpx;
			.order-card {
				position: relative;
				width: 318rpx;
				padding: 30rpx;
				border-radius: 20rpx;
				background: linear-gradient(180deg, rgba(234, 232, 255, 1) 0%, rgba(225, 225, 254, 1) 100%);
				.bg {
					position: absolute;
					right: 0;
					bottom: 0;
					width: 218rpx;
					height: 220rpx;
				}
				.label {
					color: #7B6DD1;
					font-size: 36rpx;
					font-weight: 500;
					margin-bottom: 16rpx;
				}
				.btn {
					width: 138rpx;
					height: 42rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #8D7ED7;
					font-size: 24rpx;
					border-radius: 42rpx;
					background-color: #fff;
					:deep(.nut-icon-rect-right) {
						font-size: 24rpx !important;
						width: 16rpx !important;
						height: 20rpx !important;
						font-weight: 700;
					}
				}
			}
			.right-box {
				flex: 1;
				display: flex;
				flex-direction: column;
				gap: 30rpx;
				.free-card {
					flex: 1;
					position: relative;
					padding: 30rpx;
					border-radius: 20rpx;
					background: linear-gradient(131.24deg, rgba(255, 232, 189, 1) 0%, rgba(255, 227, 161, 1) 100%);
					.bg {
						position: absolute;
						right: 0;
						bottom: 0;
						width: 140rpx;
						height: 152rpx;
					}
					.label {
						color: #FF8D03;
						font-size: 36rpx;
						font-weight: 500;
						margin-bottom: 10rpx;
					}
					.desc {
						color: #FFB66E;
						font-size: 24rpx;
						font-weight: 500;
					}
				}
				.share-card {
					flex: 1;
					position: relative;
					padding: 30rpx;
					border-radius: 20rpx;
					background: linear-gradient(90deg, rgba(255, 228, 222, 1) 0%, rgba(255, 213, 207, 1) 100%);
					.bg {
						position: absolute;
						right: 0;
						bottom: 0;
						width: 132rpx;
						height: 154rpx;
					}
					.label {
						color: #FF617E;
						font-size: 36rpx;
						font-weight: 500;
						margin-bottom: 10rpx;
					}
					.desc {
						color: #FF91A5;
						font-size: 24rpx;
						font-weight: 500;
					}
				}
			}
		}
	}
	.content-box {
		overflow: hidden;
		position: relative;
		margin-top: -30rpx;
		border-radius: 30rpx 30rpx 0 0;
		background: linear-gradient(180deg, #FFFFFF 0%, #EDF3FF 100%);
		:deep(.nut-tabs) {
			height: 100%;
			display: flex;
			flex-direction: column;
			.nut-tabs__content {
				flex: 1;
				min-height: 0;
				.nut-tab-pane {
					position: relative;
					background-color: transparent;
					padding: 28rpx 30rpx;
				}
			}
		}
		:deep(.nut-tabs__titles) {
			height: 140rpx !important;
			background-color: transparent;
			.nut-tabs__list {
				padding: 0 100rpx;
				.nut-tabs__titles-item__text {
					font-size: 36rpx;
				}
				.nut-tabs__titles-item__line {
					border-radius: 6rpx;
					height: 16rpx !important;
					background: #FFC300 !important;
				}
			}
		}
	}
	.card-item {
		overflow: hidden;
		border-radius: 20rpx;
		background-color: #fff;
		margin-bottom: 28rpx;
		&:last-of-type {
			margin-bottom: 0;
		}
		.header {
			height: 76rpx;
			display: flex;
			align-items: center;
			background-color: #e6e6ff;
			.name {
				flex: 1;
				margin-left: 40rpx;
				font-size: 32rpx;
				font-weight: 700;
			}
			.status {
				width: 248rpx;
				height: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				font-weight: 700;
				color: #fff;
				border-radius: 0 0 0 10rpx;
				background-color: #B0B0B0;
			}
			.underway {
				background-color: #8F5EFF;
			}
		}
		.content {
			padding: 32rpx 40rpx;
			color: #8A8A8A; 
			font-size: 24rpx;
		}
		:deep(.nut-divider) {
			margin: 0 24rpx;
		}
		.footer {
			height: 112rpx;
			padding: 0 20rpx 0 40rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			gap: 16rpx;
			.footer-left {
				display: flex;
				align-items: center;
				gap: 10rpx;
				.label {
					color: #828282;
					font-size: 24rpx;
				}
				.value {
					color: #FF8D1A;
					font-size: 28rpx;
					font-weight: 700;
				}
			}
			.footer-right {
				display: flex;
				align-items: center;
				gap: 20rpx;
				:deep(.btn-look .nut-button) {
					height: 60rpx;
				}
				:deep(.btn-share .nut-button) {
					height: 60rpx;
					color: #8C4600;
					border: 4rpx solid #FF8000;
					background-color: #FFA600 !important;
				}
			}
		}
	}
</style>
